package util;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import com.example.administrator.text.AppointTimeActivity;

import org.greenrobot.eventbus.EventBus;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import app.MyApplication;

/**
 * Created by liuxiaobin on 2017/5/3
 *
 */

public class WiFiShowFV {
    private TextView mTV;//按钮
    private int mType; // 0 代表new时 1代表有对象时  应用如果刚new时 它会马上执行 handler.sendMessage(message);操作
    private Timer mTimer = new Timer();//倒计时
    private Context mContext;
    private TimerTask mTask;
    private static boolean mPauseTime;
    private boolean mNotification = true;
    private static WiFiShowFV mWiFiShowFV;
    private static long mIllisInFuture,mTime,mMinute;//总时间

    final Handler mHandler = new Handler(){
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    LogUtil.eE("MTime","mTime.."+mTime+"..mMinute..."+mMinute+"....mTimer..."+mTimer);
                    if(mType == 0){
                        mType = 1;
                        return;
                    }
                    if(MyApplication.getApp().getSiteAreaid() >= 10000){
                        if(mMinute == 0){
                            if(mTime == 0){
                                HashMap<String,Object> hashMap = new HashMap<>();
                                hashMap.put("code","WiFi");
                                hashMap.put("target",AppointTimeActivity.mNetWorkstate);
                                hashMap.put("sign",AppointTimeActivity.mbreakNetWorkstate);
                                String stringJson = StringUtil.getStringUtilNew().hashMapToJson(hashMap); //参数包
                                EventBus.getDefault().post(stringJson);
                                onPause();
                            }
                            if(mTime > 0){
                                mTime --;
                                mMinute = 59;
                                setTV();
                            }
                        }else {
                            mMinute--;
                            if(mTime == 0){
                                if(mMinute == 0){
                                    onPause();
                                    HashMap<String,Object> hashMap = new HashMap<>();
                                    hashMap.put("code","WiFi");
                                    hashMap.put("target",AppointTimeActivity.mNetWorkstate);
                                    hashMap.put("sign",AppointTimeActivity.mbreakNetWorkstate);
                                    String stringJson = StringUtil.getStringUtilNew().hashMapToJson(hashMap); //参数包
                                    EventBus.getDefault().post(stringJson);
                                }else {
                                    if(StringUtil.getStringUtilNew().judgeUIType(MyApplication.getApp().getmPattern()) == 1){
                                        if(mMinute <= 9){
                                            mTV.setText("剩余时间 00:0"+mMinute);
                                            if(mMinute < 6 && mNotification){
                                                mNotification = false;
                                                //setNotification();
                                            }
                                        }else {
                                            mTV.setText("剩余时间 00:"+mMinute);
                                        }
                                    }else {
                                        onShop();
                                    }
                                }
                            }else {
                                setTV();
                            }
                        }

                    }else {
                        onPause();
                    }
            }
        }
    };

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private WiFiShowFV(Context context){
        mContext = context.getApplicationContext();
    }
    public static WiFiShowFV newInstance(Context context){
        if(mWiFiShowFV == null){
            synchronized (WiFiShowFV.class){
                if(mWiFiShowFV == null){
                    mWiFiShowFV = new WiFiShowFV(context);
                    return mWiFiShowFV;
                }
            }
        }
        return mWiFiShowFV;
    }
    /**
     * @param illisInFuture 总时间
     * @param tv 对应的控件
     */
    public void showTv(int type,long illisInFuture, final TextView tv){
        this.mType = type;
        mIllisInFuture = illisInFuture;
        this.mTV = tv;

        if(mTimer == null){mTimer = new Timer();}

        mTask = new TimerTask() {
            @Override public void run() {
                LogUtil.eE("MTime","................run............."+mPauseTime);
                if(mPauseTime){
                    Message message = new Message();
                    message.what = 1;
                    mHandler.sendMessage(message);
                }
            }
        };
        onRecovery();
        if(mIllisInFuture == 59){mIllisInFuture = 60;}
        if(mIllisInFuture == 119){mIllisInFuture = 120;}
        if(mIllisInFuture == 179){mIllisInFuture = 180;}
        mMinute = mIllisInFuture % 60;
        mTime = mIllisInFuture / 60;

        LogUtil.eE("MTime","mTime.."+mTime+"..mMinute..."+mMinute);
        if(mMinute >=0 && mTime >=0){
           setTV();
            if(mTimer !=null){
                mTimer.schedule(mTask,1000,60000);       //循环执行
            }
        }

    }

    /**
     * 改变tv
     */
    public void changeTV(long i){
        if(i == 59){i = 60;}
        if(i == 119){i = 120;}
        if(i == 179){i = 180;}
        LogUtil.eE("","changeTV.........."+i);
        mTime = i /60 ;
        mMinute = i % 60;
        LogUtil.eE("","changeTV....mTime......"+mTime+"..mMinute.."+mMinute);
        setTV();
        onRecovery();
        if(mTimer == null){
            mTimer = new Timer();
            mTimer.schedule(mTask,1000,60000);
        }
    }

    /**
     * 停止
     */
    public void onShop(){
        if(mTimer !=null){
            mTimer.purge();
            mTimer.cancel();
            mTimer = null;
        }
    }

    /**
     * 暂停
     */
    public void onPause(){
        if(mTimer !=null){
            LogUtil.eE("",".....onPause..........");
            mPauseTime = false;
        }
    }

    /**
     * 恢复
     */
    private void onRecovery(){
       mPauseTime = true;
       mNotification = true;
    }

    private void setTV(){
        if(MyApplication.getApp().getSiteAreaid() !=0){
            if(StringUtil.getStringUtilNew().judgeUIType(MyApplication.getApp().getmPattern()) == 1){
                if(mTime <= 9){
                    if(mMinute <= 9){
                        mTV.setText("剩余时间 0" +mTime+":0"+mMinute);
                    }else {
                        mTV.setText("剩余时间 0" +mTime+":"+mMinute);
                    }
                }else {
                    if(mMinute <= 9){
                        mTV.setText("剩余时间  " +mTime+":0"+mMinute);
                    }else {
                        mTV.setText("剩余时间  " +mTime+":"+mMinute);
                    }
                }
            }else {
                onShop();
            }
        }else {
            onPause();
        }
    }
}
